package com.jswlw.controller.admin.biz;

import com.github.pagehelper.Page;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.DevFilter;
import com.jswlw.service.biz.DevFilterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin/biz/devFilter")
public class AdminDevFilterController extends BaseController {

    @Autowired
    DevFilterService service;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/devFilter/index";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody DevFilter devFilter) {
        Page<DevFilter> page = service.queryPage(devFilter, devFilter.getOffset(), devFilter.getLimit());
        return pageResult(page.getResult(), page.getTotal());
    }

    /**
     * 新增广告
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("biz/devFilter/edit");
        return modelAndView;
    }

    /**
     * 编辑广告
     *
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info", service.find4Id(id));
        modelAndView.setViewName("biz/devFilter/edit");
        return modelAndView;
    }

    /**
     * 保存广告
     *
     * @param devFilter
     * @param
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(DevFilter devFilter) {
        try {
            if (devFilter.getId() == null) {
                service.add(devFilter);
            } else {
                service.update(devFilter);
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    /**
     * 删除广告
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ModelMap delete(Model model, Integer id) {
        try {
            service.delete(id);
            return this.successResult("删除成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("删除失败" + e.getMessage());
        }
    }
}
